<?php
namespace App\Model\Face;

use App\Model\Base;

class Face extends Base{
	public $tableName = "mymk_face";

	/**
	 * 通过条件获取数据库里面的用户
	 * @auth   xiaozhi
	 * @param  array   $condition [description]
	 * @return [type]             [description]
	 */
	public function getFaceData($field = '*', $where = [],  $page = 1, $pageSize =10, $order = ['A.id', 'desc'], $find='') {

		$cons = $this->withTotalCount()
		    ->field($field)
		    ->order($order[0], $order[1])
		    ->alias('A')
			->join('mymk_face_info as B','A.id=B.faceid','LEFT')
			->where($where);
		if($find){
		    $cons->where("find_in_set({$find}, departmentid)")
                ->page($page, $pageSize)
                ->all();
        }else{
		    $cons->page($page, $pageSize)
                ->all();
        }
		$count = $this->lastQueryResult()->getTotalCount();
        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return ['count' => $count, 'lists' => $lists];
	}


	/**
	 * 根据条件获取人脸更新列表数据
	 * @param  [type] $columns [description]
	 * @param  [type] $where   [description]
	 * @param  [type] $limit   [description]
	 * @return [type]          [description]
	 */
	public function getFaceInfo($field = '*', $where = []){

		$this->withTotalCount()
		    ->field($field)
		    ->alias('A')
			->join('mymk_face_info as B','A.id=B.faceid','LEFT')
			->where($where)
            ->all();
		

        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return $lists;
	}

	/**
	 * 查询是否存在访客记录
	 * @param  string $field     [description]
	 * @param  [type] $where     [description]
	 * @param  [type] $starttime [description]
	 * @param  [type] $endtime   [description]
	 * @return [type]            [description]
	 */
	public function getVisitorIsset($field = 'A.id', $where, $starttime, $endtime){
		$this->withTotalCount()
		    ->field($field)
		    ->alias('A')
			->join('mymk_face_info as B', 'A.id=B.faceid', 'LEFT')
			->where($where)
			->where("((B.starttime between $starttime and $endtime) or (B.endtime between $starttime and $endtime)) ")
            ->get();
		

        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return $lists;
	}

	/**
	 * [getAuth description]
	 * @param  string $field [description]
	 * @param  [type] $where [description]
	 * @return [type]        [description]
	 */
	public function getAuth($field = '*', $where = []){
		$this->withTotalCount()
		    ->field($field)
		    ->alias('A')
			->join('mymk_admin_user as B','A.mobile=B.mobile','LEFT')
			->where($where)
            ->all();
		

        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return $lists;
	}

	/**
	 * 查询员工考勤排班数据
	 * @auth   xiaozhi
	 * @param  array   $condition [description]
	 * @return [type]             [description]
	 */
	public function getScheduleData($field = '*', $where = [], $year=2020, $month=12, $page = 1, $pageSize =10, $order = ['A.id', 'desc']) {
		$this->withTotalCount()
		    ->field($field)
		    ->order($order[0], $order[1])
		    ->alias('A')
			->join('mymk_attend_schedule as B',"A.id=B.faceid and A.companyid=B.companyid and B.year={$year} and B.month={$month}",'LEFT')
			->where($where)
            ->page($page, $pageSize)
            ->all();
		

		$count = $this->lastQueryResult()->getTotalCount();
        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return ['count' => $count, 'lists' => $lists];
	}

	/**
	 * 查询员工考勤排班数据
	 * @auth   xiaozhi
	 * @param  array   $condition [description]
	 * @return [type]             [description]
	 */
	public function getAllSchedule($field = '*', $where = [], $year = 2020, $month = 12) {
		$this->withTotalCount()
		    ->field($field)
		    ->alias('A')
			->join('mymk_attend_schedule as B',"A.id=B.faceid and A.companyid=B.companyid and B.year={$year} and B.month={$month}",'LEFT')
			->where($where)
            ->all();
		

        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return $lists;
	}

    /**
     * 查询员工考勤排班数据
     * @auth   xiaozhi
     * @param  string  $field [description]
     * @param  array   $where [description]
     * @return [type]             [description]
     */
	public function getFaceDep($field = '*', $where = []){
        $this->withTotalCount()
            ->field($field)
            ->group('A.id')
            ->alias('A')
            ->join('mymk_department as B',"find_in_set(B.id, A.departmentid)",'LEFT')
            ->where($where)
            ->all();

        $lists = $this->lastQueryResult()->getResult();

        // $list = $lastResult->getResult();
        return $lists;
    }
}